import 'package:flutter/material.dart';
import 'package:flutter_test_qq/main.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'dataBase.dart';
class registerPage extends StatefulWidget{

  const registerPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState()=>registerPageState();

}
var qqId = '';
var qqname = '';
var qqpwd = '';
var qqpic = '';
String picUrl = 'images/pic4.jpg';

class registerPageState extends State<registerPage>{
  TextEditingController id = new TextEditingController();
  TextEditingController name = new TextEditingController();
  TextEditingController pwd = new TextEditingController();
  TextEditingController pwd2 = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        resizeToAvoidBottomInset:  false,
        appBar: AppBar(
            backgroundColor:Colors.lightGreen,
            leading: IconButton(
            icon: Container(
              padding: EdgeInsets.all(1.0),
              child: Icon(Icons.keyboard_arrow_left),
            ),
            onPressed: (){
              Navigator.of(context).pop();
            },
          ),
          title: Text('QQ注册',style:TextStyle(fontSize: 20,),),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(height: 10,),
              InkWell(
                  onTap: () {
                    showDialog(
                        context:context,
                        builder:(BuildContext context) => Dialog(
                          child:  Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Text(
                                  "选择头像",
                                  style: TextStyle(fontSize: 25),
                                ),
                                Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceEvenly,
                                  children: [
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic1.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic1.jpg")),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic2.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic2.jpg")),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic3.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic3.jpg")),
                                    ),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceEvenly,
                                  children: [
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic4.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic4.jpg")),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic5.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic5.jpg")),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic6.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic6.jpg")),
                                    ),
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment:
                                  MainAxisAlignment.spaceEvenly,
                                  children: [
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic7.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic7.jpg")),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic8.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic8.jpg")),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        setState(() {
                                          picUrl = "images/pic9.jpg";
                                        });
                                        Navigator.pop(context);
                                      },
                                      child: CircleAvatar(
                                          backgroundImage: AssetImage(
                                              "images/pic9.jpg")),
                                    ),
                                  ],
                                )
                              ],
                            ),
                          ),
                        )
                    );
                  },
                  child: CircleAvatar(
                    backgroundImage: AssetImage(picUrl),
                    maxRadius: 40,
                  )),
              //  qq号输入
              SizedBox(height: 10,),
              Padding(
                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: SizedBox(
                    child: TextField(
                      controller: id,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        hintText: 'QQ号',
                        filled: true,
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00BB0000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00000000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                      ),
                    ),
                    width: 340,
                  )
              ),
              //  昵称输入
              SizedBox(height: 10,),
              Padding(
                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: SizedBox(
                    child: TextField(
                      controller: name,
                      decoration: InputDecoration(
                        hintText: '昵称',
                        filled: true,
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00BB0000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00000000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                      ),
                    ),
                    width: 340,
                  )
              ),
              //  密码输入
              SizedBox(height: 10,),
              Padding(
                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: SizedBox(
                    child:  TextField(
                      controller: pwd,
                      obscureText: true,
                      decoration: InputDecoration(
                        hintText: '密码',
                        filled: true,
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00BB0000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00000000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                      ),
                    ),
                    width: 340,
                  )
              ),
              //  确认密码
              SizedBox(height: 10,),
              Padding(
                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: SizedBox(
                    child: TextField(
                      controller: pwd2,
                      obscureText: true,
                      decoration: InputDecoration(
                        hintText: '确认密码',
                        filled: true,
                        enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00BB0000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0x00000000)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(15),
                          ),
                        ),
                      ),
                    ),
                    width: 340,
                  )
              ),

              //  注册按钮
              SizedBox(height: 10,),
              MaterialButton(
                child: new Text('注册'),
                color: Colors.lightGreen,
                onPressed: () {
                  if(pwd2.text != pwd.text){
                    showDialog<Null>(
                        context: context,
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          return new AlertDialog(
                            content: new SingleChildScrollView(
                              child: new ListBody(
                                children: <Widget>[
                                  new Text('两次密码输入不一致'),
                                ],
                              ),
                            ),
                            actions: <Widget>[
                              new ElevatedButton(
                                child: new Text('确定'),
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                              ),
                            ],
                          );
                        });
                  }
                  else{
                    String regExp ="^(?![0-9]+\$)(?![a-zA-Z]+\$)[0-9A-Za-z]{8,24}\$";
                    // print(RegExp(regExp).hasMatch(pwd2.text));
                    if(RegExp(regExp).hasMatch(pwd2.text) == true){
                      qqId = id.text;
                      qqname = name.text;
                      qqpwd = pwd2.text;
                      qqpic = picUrl;
                      // print('qqname:'+qqname);
                      // print('qqPwd:'+qqpwd);
                      queryDatabase(context);
                    }
                    else if((RegExp(regExp).hasMatch(pwd2.text) == false && pwd2.text.length>24)
                        ||(RegExp(regExp).hasMatch(pwd2.text) == false && pwd2.text.length<8)
                    ){
                      Fluttertoast.showToast(msg: '请确保密码长度≥8并≤24',textColor: Colors.white);
                    }
                    else{
                      Fluttertoast.showToast(msg: '请确保密码同时且只含有字母和数字',textColor: Colors.white);
                    }
                  }
                },
              ),
            ],
          ),
        )


      ),
    );
  }
  queryDatabase(BuildContext context) async {
    TableTestProvider testProvider = TableTestProvider();
    //  通过getDatabasePath()方法获取数据库位置
    var databasePath = await getDatabasesPath();
    String path = join(databasePath, 'demo.db');
    // deleteDatabase(path);
    // await testProvider.create(path);
    await testProvider.open(path);
    // // 查询数据
    Test dd = await testProvider.getTodo(qqId);
    print('查询${qqId}:${dd.toMap()}');
    if(dd.id!=null){
      print('qq号已存在');
      Fluttertoast.showToast(msg: 'QQ号已存在',textColor: Colors.white);
    }
    else{
      // 新增注册数据
      Test test1 = Test();
      test1.id = qqId;
      test1.name = qqname;
      test1.pwd = qqpwd;
      test1.pic = qqpic;
      Test td = await testProvider.insert(test1);
      print('注册成功:${td.toMap()}');
      Fluttertoast.showToast(msg: '注册成功！！',textColor: Colors.white);

      Navigator.of(context)
          .push(MaterialPageRoute(builder: (BuildContext context) {
        return Login();
      }));
    }
    // 关闭数据库
    testProvider.close();
  }

}